package com.pathfinder;

import android.util.Pair;
import com.pathfinder.model.Game;
import com.pathfinder.model.Player;
import com.pathfinder.utils.Log;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.pathfinder.model.Serialization.toJSON;
import static com.pathfinder.utils.ClientUtils.makePostRequest;

/**
 * Date: 10.05.11
 *
 * @author Victor Passichenko (vpas@yandex-team.ru)
 */
public class RequestSender {
    private final static String SERVER_URL = "http://pathfindersgame.appspot.com";

    private final static String CREATE_GAME_PATH = "/game/create";
    private final static String JOIN_GAME_PATH = "/game/join";
    private final static String GAME_LIST_PATH = "/game/list";
    private final static String UPDATE_PLAYER_INFO_PATH = "/game/player/info/update";
    private final static String PLAYER_SET_IS_READY_PATH = "/game/player/set_is_ready";
    private final static String GET_GAME_STATE_PATH = "/game/state/get";
    private final static String UPDATE_LOCATION_PATH = "/location/update";

    public static boolean sendCreateGameRequest(Game game, Player player) {
        String url = SERVER_URL + CREATE_GAME_PATH;

        List<Pair<String, String>> params = new ArrayList<Pair<String, String>>();

        String jsonGame = toJSON(game);
        String jsonPlayerInfo = toJSON(player.getInfo());

        params.add(Pair.create("game", jsonGame));
        params.add(Pair.create("player_info", jsonPlayerInfo));

        String response = makePostRequest(url, params);

        if (response != null) {
            try {
                JSONObject jsonResponse = new JSONObject(response);
                game.setId(jsonResponse.getInt("game_id"));
                player.setId(jsonResponse.getInt("player_id"));
                return true;
            } catch (JSONException e) {
                Log.error("Cannot parse " + response, e);
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean sendJoinGameRequest(Game game, Player player) {
        String url = SERVER_URL + JOIN_GAME_PATH;

        List<Pair<String, String>> params = new ArrayList<Pair<String, String>>();

        String jsonPlayerInfo = toJSON(player.getInfo());

        params.add(Pair.create("game_id", String.valueOf(game.getId())));
        params.add(Pair.create("player_info", jsonPlayerInfo));

        String response = makePostRequest(url, params);

        if (response != null) {
            try {
                JSONObject jsonResponse = new JSONObject(response);
                player.setId(jsonResponse.getInt("player_id"));
                return true;
            } catch (JSONException e) {
                Log.error("Cannot parse " + response, e);
                return false;
            }
        } else {
            return false;
        }
    }

    public static void sendPlayerSetIsReadyRequest(Player player) {
        // TODO
    }

    public static void sendUpdatePlayerInfoRequest(Player player){
        // TODO
    }
}
